package com.meessiom.train.crm.RepaymentSchedule.service;

import com.meessiom.train.crm.RepaymentSchedule.model.RepaymentScheduleInfo;
import com.meessiom.train.crm.RepaymentSchedule.repository.RepaymentScheduleRepository;
import com.meessiom.train.crm.common.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepaymentScheduleInfoService {
    @Autowired
    private RepaymentScheduleRepository repaymentScheduleRepository;

    public List<RepaymentScheduleInfo> saveAll(List<RepaymentScheduleInfo> repaymentSchedules) {
        return repaymentScheduleRepository.saveAll(repaymentSchedules);
    }

    public List<RepaymentScheduleInfo> getAllRepaymentSchedules() {
        return  repaymentScheduleRepository.findAll();
    }

    public RepaymentScheduleInfo createRepaymentSchedule(RepaymentScheduleInfo repaymentScheduleInfo){
        return repaymentScheduleRepository.save(repaymentScheduleInfo);
    }

    public RepaymentScheduleInfo deleteRepaymentSchedule(long id){
        RepaymentScheduleInfo repaymentScheduleInfo = repaymentScheduleRepository.findById(id).get();
        repaymentScheduleRepository.deleteById(id);
        return repaymentScheduleInfo;
    }
    public RepaymentScheduleInfo getRepaymentScheduleById(long id) {
        RepaymentScheduleInfo repaymentSchedule = repaymentScheduleRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("RepaymentSchedule not exist with id" + id));
        return repaymentSchedule;
    }
    public RepaymentScheduleInfo updateRepaymentSchedule(Long id, RepaymentScheduleInfo repaymentScheduleInfo) {
        RepaymentScheduleInfo repaymentSchedule = repaymentScheduleRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("RepaymentSchedule not exist with id" + id));
        repaymentSchedule.setCustomerName(repaymentScheduleInfo.getCustomerName());
        repaymentSchedule.setPeriod(repaymentScheduleInfo.getPeriod());
        repaymentSchedule.setTotalPeriods(repaymentScheduleInfo.getTotalPeriods());
        repaymentSchedule.setPrincipal(repaymentScheduleInfo.getPrincipal());
        repaymentSchedule.setDueDate(repaymentScheduleInfo.getDueDate());
        repaymentSchedule.setStatus(repaymentScheduleInfo.getStatus());
        return repaymentScheduleRepository.save(repaymentSchedule);
    }

}
